/**
 * Copyright 2018 人人开源 http://www.flow.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.xhtt.modules.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.Query;
import com.xhtt.modules.sys.dao.SysPositionDao;
import com.xhtt.modules.sys.entity.SysPositionEntity;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysPositionService;
import com.xhtt.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("sysPositionService")
public class SysPositionServiceImpl extends ServiceImpl<SysPositionDao, SysPositionEntity> implements SysPositionService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public void deletePosition(Integer positionId) {
        int count = sysUserService.count(new QueryWrapper<SysUserEntity>().eq(SysUserEntity.POSITIONID, positionId));
        if (count > 0) {
            throw new RRException("请先删除该职位下的用户");
        }
        this.removeById(positionId);
    }

    @Override
    public PageUtils queryPositionPage(Map<String, Object> params) {
        Page<SysPositionEntity> page = new Query<SysPositionEntity>(params).getPage();
        page.setRecords(baseMapper.queryPositionPage(page, params));
        return new PageUtils(page);
    }
}
